package com.example.calendar.ui.components.events

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AccessTime
import androidx.compose.material.icons.filled.Notifications
import androidx.compose.material.icons.filled.Place
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.calendar.data.model.CalendarEvent
import com.example.calendar.ui.theme.categoryDefault
import org.threeten.bp.LocalDateTime
import org.threeten.bp.format.DateTimeFormatter

/**
 * A composable that displays a calendar event item.
 */
@Composable
fun EventItem(
    event: CalendarEvent,
    onClick: () -> Unit,
    modifier: Modifier = Modifier
) {
    Card(
        modifier = modifier
            .fillMaxWidth()
            .padding(vertical = 4.dp)
            .clickable(onClick = onClick),
        elevation = CardDefaults.cardElevation(defaultElevation = 2.dp),
        shape = RoundedCornerShape(8.dp)
    ) {
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(12.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            // Category color indicator
            Box(
                modifier = Modifier
                    .size(16.dp)
                    .clip(CircleShape)
                    .background(Color(event.color ?: categoryDefault.value.toInt()))
            )
            
            Spacer(modifier = Modifier.width(12.dp))
            
            // Event details
            Column(
                modifier = Modifier.weight(1f)
            ) {
                // Title
                Text(
                    text = event.title,
                    style = MaterialTheme.typography.titleMedium,
                    fontWeight = FontWeight.SemiBold,
                    maxLines = 1,
                    overflow = TextOverflow.Ellipsis
                )
                
                Spacer(modifier = Modifier.height(4.dp))
                
                // Time
                EventDetailRow(
                    icon = Icons.Default.AccessTime,
                    text = if (event.isAllDay) "All day" else event.getTimeRangeString()
                )
                
                // Location (if available)
                event.location?.let {
                    Spacer(modifier = Modifier.height(2.dp))
                    EventDetailRow(
                        icon = Icons.Default.Place,
                        text = it
                    )
                }
                
                // Reminder indicator (if has reminders)
                if (event.reminderMinutes.isNotEmpty()) {
                    Spacer(modifier = Modifier.height(2.dp))
                    EventDetailRow(
                        icon = Icons.Default.Notifications,
                        text = "${event.reminderMinutes.first()} minutes before"
                    )
                }
            }
        }
    }
}

@Composable
fun EventDetailRow(
    icon: ImageVector,
    text: String
) {
    Row(
        verticalAlignment = Alignment.CenterVertically
    ) {
        Icon(
            imageVector = icon,
            contentDescription = null,
            modifier = Modifier.size(16.dp),
            tint = MaterialTheme.colorScheme.onSurfaceVariant
        )
        
        Spacer(modifier = Modifier.width(4.dp))
        
        Text(
            text = text,
            style = MaterialTheme.typography.bodySmall,
            color = MaterialTheme.colorScheme.onSurfaceVariant,
            maxLines = 1,
            overflow = TextOverflow.Ellipsis
        )
    }
}

@Preview(showBackground = true)
@Composable
fun EventItemPreview() {
    val sampleEvent = CalendarEvent(
        id = "1",
        title = "Team Meeting",
        description = "Weekly team sync",
        location = "Conference Room A",
        startDateTime = LocalDateTime.now(),
        endDateTime = LocalDateTime.now().plusHours(1),
        reminderMinutes = listOf(15),
        color = categoryDefault.value.toInt()
    )
    
    MaterialTheme {
        EventItem(
            event = sampleEvent,
            onClick = {},
            modifier = Modifier.padding(16.dp)
        )
    }
} 